<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Recharge;
use App\Models\User;
use App\Models\Wallet;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;


class RechargeController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('充值管理')
            ->description('充值管理')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Recharge);

        $grid->model()->where('status', 0);
        $grid->id('Id');
        $grid->user_id('用户昵称')->display(function ($value) {
            $user = User::where('id', $value)->value('nickname');
            if ($user) {
                return $user;
            }
        });
        $grid->money('充值金额');
        $grid->type('充值渠道')->display(function ($value) {
            switch ($value) {
                case '0':
                    return "<span class='label label-success'>人工充值</span>";
                    break;
                case '1':
                    return "<span class='label label-success'>微信</span>";
                    break;
                case '2':
                    return "<span class='label label-success'>支付宝</span>";
                    break;

                default:
                    # code...
                    break;
            }
        });
        $grid->status('充值状态')->display(function ($value) {
            switch ($value) {
                case '0':
                    return "<span class='label label-warning'>待审核</span>";
                    break;
                case '1':
                    return "<span class='label label-success'>已通过</span>";
                    break;
                case '2':
                    return "<span class='label label-primary'>已取消</span>";
                    break;
                case '3':
                    return "<span class='label label-danger'>未通过</span>";
                    break;
                default:
                    # code...
                    break;
            }
        });
        $grid->created_at('创建时间');
        $grid->updated_at('更新时间');

        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();
        });

        // $grid->disableActions();
        $grid->disableExport();
        $grid->disableRowSelector();
        $grid->disableCreateButton();

        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->equal('type', '充值渠道')->radio([
                0 => '人工',
                1 => '微信',
                2 => '支付宝',
            ]);
            // $filter->scope('待审核')->where('status', 0);
            $filter->scope('今日新增', '今日新增')->whereDate('created_at', date('Y-m-d'));
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Recharge::findOrFail($id));

        $show->id('Id');
        $show->user_id('User id');
        $show->money('Money');
        $show->type('Type');
        $show->status('Status');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Recharge);

        $form->number('user_id', 'User id');
        $form->decimal('money', 'Money');
        $form->number('type', 'Type');
        $form->number('status', 'Status');

        return $form;
    }

    

}
